package com.ugoodtech.mdcc.web.service.impl;

import com.mysema.query.BooleanBuilder;
import com.ugoodtech.mdcc.core.domain.QQuestionPaper;
import com.ugoodtech.mdcc.core.domain.QuestionPaper;
import com.ugoodtech.mdcc.core.exception.VStarException;
import com.ugoodtech.mdcc.core.repository.QuestionPaperRepository;
import com.ugoodtech.mdcc.web.service.QuestionPaperService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

@Service
public class QuestionPaperServiceImpl implements QuestionPaperService {

    @Autowired
    private QuestionPaperRepository questionPaperRepository;


    @Override
    public Page<QuestionPaper> queryQuestionPaper(QuestionPaper.QuestionPaperCategory category, Pageable pageable) {
        QQuestionPaper qQuestionPaper = QQuestionPaper.questionPaper;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qQuestionPaper.deleted.eq(false));
        if (null!=category) {
            builder.and(qQuestionPaper.category.eq(category));
        }
        Page<QuestionPaper> questionPapers = questionPaperRepository.findAll(builder,pageable);
        return questionPapers;
    }

    @Override
    public QuestionPaper createQuestionPaper(@ApiParam("问卷名字") String name,
                                             @ApiParam("创建原因") @RequestParam String reason,
                                             @ApiParam("问卷分类") QuestionPaper.QuestionPaperCategory category,
                                             @ApiParam("版本号") String version) throws VStarException {
        QuestionPaper questionPaper=new QuestionPaper();
        questionPaper.setName(name);
        questionPaper.setReason(reason);
        questionPaper.setCategory(category);
        questionPaper.setVersion(version);
        return questionPaperRepository.save(questionPaper);
    }
}
